<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>权限列表</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="__ADMIN_STATIC__/css/oksub.css">
	<script type="text/javascript" src="__STATIC__/lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--模糊搜索区域-->
	<form class="layui-form ok-search-form">
		<div class="layui-form-item">
			<div class="layui-inline">
				<div class="layui-input-inline">
					<input type="text" class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime" name="start_time">
				</div>
			</div>
			<div class="layui-inline">
				<div class="layui-input-inline">
					<input type="text" class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime" name="end_time">
				</div>
			</div>
			<div class="layui-inline">
				<div class="layui-input-inline">
					<select name="type" lay-verify="">
						<option value="" selected>请选择类型</option>
						<option value="1">角色</option>
						<option value="2">权限</option>
					</select>
				</div>
			</div>
			<div class="layui-inline">
				<div class="layui-input-inline">
					<button class="layui-btn" lay-submit="" lay-filter="search">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
		</div>
	</form>
	<!--数据表格-->
	<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="__STATIC__/lib/layui/layui.js"></script>
<script>
	layui.use(["table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
		let table = layui.table;
		let form = layui.form;
		let util = layui.util;
		let laydate = layui.laydate;
		let okLayer = layui.okLayer;
		let okUtils = layui.okUtils;
		okLoading.close();
		util.fixbar({});

		laydate.render({elem: "#startTime", type: "datetime"});
		laydate.render({elem: "#endTime", type: "datetime"});

		let articleTable = table.render({
			elem: "#tableId",
			url: "{:url('permission/list')}",
			limit: 20,
			page: true,
			even: true,
			toolbar: "#toolbarTpl",
			size: "lg",
			cols: [[
				{type: "checkbox", fixed: "left"},
				{field: "id", title: "ID", width: 100, sort: true},
				{field: "name", title: "权限名称", width: 200},
				{field: "type", title: "类型", width: 100, templet: "#typeTpl"},
				{field: "rule", title: "规则", width: 200},
				{field: "desc", title: "描述", width: 400},
				{field: "create_time", title: "创建时间", width: 180},
				{field: "update_time", title: "更新时间", width: 180},
				{title: "操作", width: 100, align: "center", fixed: "right", templet: "#operationTpl"}
			]],
			done: function (res, curr, count) {
				console.log(res, curr, count)
			}
		});

		form.on("submit(search)", function (data) {
			articleTable.reload({
				where: data.field,
				page: {curr: 1}
			});
			return false;
		});

		table.on("toolbar(tableFilter)", function (obj) {
			switch (obj.event) {
				case "addRoute":
					addRoute();
					break;
				case "addRole":
					addRole();
					break;
				case "batchDelete":
					batchDelete();
					break;
			}
		});

		table.on("tool(tableFilter)", function (obj) {
			let data = obj.data;
			switch (obj.event) {
				case "updateById":
					updateById(data.id);
					break;
				case "deleteById":
					deleteById(data.id);
					break;
				case "relateById":
					relateById(data.id);
					break;
			}
		});


		function batchDelete() {
			okLayer.confirm("确定要批量删除吗？", function (index) {
				layer.close(index);
				let idsStr = okUtils.tableBatchCheck(table);
				if (idsStr) {
					okUtils.ajax("{:url('permission/batchDel')}", "delete", {ids: idsStr}, true).done(function (response) {
						okUtils.tableSuccessMsg(response.msg);
					}).fail(function (error) {
						console.log(error);
					});
				}
			});
		}

		function addRoute() {
			okLayer.open("管理路由", "{:url('permission/createRoute')}", "90%", "90%", null, function () {
				articleTable.reload();
			})
		}
		function addRole() {
			okLayer.open("添加角色", "{:url('permission/create')}", "90%", "90%", null, function () {
				articleTable.reload();
			})
		}

		function updateById(id) {
			okLayer.open("编辑权限", "{:url('permission/edit')}?id=" + id, "90%", "90%", null, function () {
				articleTable.reload();
			})
		}
		function relateById(id) {
			okLayer.open("角色授权", "{:url('permission/relate')}?id=" + id, "90%", "90%", null, function () {
				articleTable.reload();
			})
		}

		function deleteById(id) {
			okLayer.confirm("确定要删除吗？", function () {
				okUtils.ajax("{:url('permission/delete')}", "delete", {id: id}).done(function (response) {
					okUtils.tableSuccessMsg(response.msg);
				}).fail(function (error) {
					console.log(error);
				});
			})
		}
	})
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDelete">批量删除</button>
		<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="addRoute">管理路由</button>
		<button class="layui-btn layui-btn-sm" lay-event="addRole">添加角色</button>
	</div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
	{{#  if(d.type == 1){ }}
	<a href="javascript:" title="授权" lay-event="relateById"><i class="layui-icon">&#xe654;</i></a>
	{{#  } }}
	<a href="javascript:" title="编辑" lay-event="updateById"><i class="layui-icon">&#xe642;</i></a>
	<a href="javascript:" title="删除" lay-event="deleteById"><i class="layui-icon">&#xe640;</i></a>
</script>

<script type="text/html" id="typeTpl">
	{{#  if(d.type == 1){ }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">角色</span>
	{{#  } else if(d.type == 2) { }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">路由</span>
	{{#  } }}
</script>
</body>
</html>
